package com.wx.springboot.step6.web;

import com.wx.springboot.step6.dto.UserDto;
import com.wx.springboot.step6.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * thymeleaf demo
 */
@Controller
@Api(tags = "登录管理")
public class LoginController {

    @ResponseBody
    @PostMapping("/login")
    @ApiOperation("登录")
    public Map<String,String> login(UserDto userDto) {
        Map<String,String> result = new HashMap<>();
        result.put("result","success");
        if (userDto == null || StringUtils.isEmpty(userDto.getName()) || userDto.getName().length() < 2) {
            result.put("result","fail");
        } else {
            result.put("url","home");
        }
        return result;
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/list")
    public String list(ModelMap map) {
        List<UserVo> list = new ArrayList<>();
        UserVo userVo1 =new UserVo("张三",12,"哇哈哈哈哈");
        UserVo userVo2 =new UserVo("李四",6,"小朋友");
        UserVo userVo3 =new UserVo("王二麻子",66,"666666");
        list.add(userVo1);
        list.add(userVo2);
        list.add(userVo3);
        map.addAttribute("users", list);
        map.addAttribute("message", list);
        return "list";
    }
}
